function [LaserWL, ramanpeak, LaserWLgauss] = cyclohexcalibration(datasetname, SIF)

% [LaserWL] = cyclohexcalibration(datsetname)
%
% This function uses the spectrum of Cyclohexane to return the exact
% wavelength of the exitation Laser to be used for subsequent calibration
% of actual Raman spectra.
%
% INPUT:    datasetname     =   Name of the C.H.-spectra file
%           SIF             =   if set 1 input will be treated as ANDOR
%                                  *.sif file and parset to SIFREAD first
%
% OUTPUT:   LaserWL         =   Wavelength of exitation Laser 
%           ramanpeak       =   Raman peak chosen for calibration by user

if nargin<1,
    help cyclohexcalibration
    return
end

%% -----==================== CONFIG ====================-----
% ----- Set the reference peaks -----
ramanpeak0 = 801.3;         % put here in cm-1
ramanpeak1 = 1028.3;         % put here in cm-1
ramanpeak2 = 1157.6;         % put here in cm-1
ramanpeak3 = 1266.4;         % put here in cm-1
ramanpeak4 = 1444.4;         % put here in cm-1
ramanpeak5 = 2852.9;         % put here in cm-1
ramanpeak = ramanpeak1;

% Reference spectrum
refx = [750 ...
        770 791 801.3 811 830 ...
        1000 1018 1028.3 1038 1060 ...
        1130 1147 1157.6 1167 1180 ...
        1240 1246 1256 1260 1266.4  1272 1290 ... 
        1340 1350 1360 ...
        1415 1434 1444.4 1454 1470 ...
        2650 2655 2660 2664 2666 2673 2677 2690 ...
        2830 2842 2847 2852.9 2862 2869 2877 2890 2900 2910 2930 2938 2950 2970];
refy = [0 ...
        0 5 60 5 0 ...
        0 5 30 5 0 ...
        0 3 10 3 0 ...
        0 5 25 20 23 5 0 ...
        0 5 0 ...
        0 5 25 5 0 ...
        0 7 1 10 3 7 1 0 ...
        0 5 15 70 20 15 22 20 50 30 28 55 5 0];

% sets point to fit on Gaussian    
pointsongaussianEdit = 50;


%% -----==================== CODE ====================-----
ramanpeak = ramanpeak/10000000;

%% Check for identity of input format
if nargin==2
    if SIF == 1
        rc = loadsif_nonGUI(datasetname, 1);
        if rc ~= 0
            errordlg('''SIF''could not be read!!!')
            return
        end
        disp(['Sif read with the RC ',num2str(rc)])
    else
        errordlg('Variable ''SIF'' was not set correctly!!')
        return
    end
end


%% load *.mat data
%disp('start load mat')
try load(datasetname)
catch ME
  errordlg('ERROR: file not found in "cyclohexcalibration"!')
  return
end
load(datasetname)

% ----- pick peak -----
scrsz = get(0,'ScreenSize');
chooser = figure('OuterPosition',...
                [scrsz(3)*0.05 (scrsz(4)*0.05) ...
                    scrsz(3)*0.9 scrsz(4)*0.9]);
    subplot(2,1,1);
    plot(datax,datay); 
%    h = zoom;   
%    zoom
%     q = msgbox({'Zoom in on the most prominent peak, please...' '' '' ...
%                 'Press OK when the peak maximum is easy to click.'},...
%                 'Zoom in on Peak');
%     posq = get(q,'Position'); posq(1,2) = 100;
%         set(q, 'Position', posq);
%     uiwait(q)
%    setAllowAxesZoom(h,refgraph,false);
    q = msgbox('Click on the center of the peak, please...',...
                'Choose Peak', 'modal');
        posq = get(q,'Position'); posq(1,2) = 100;
            set(q, 'Position', posq);
    uiwait(q)

    [x y] = ginput(1);
    text(x, y,'selected peak','Color','r','EdgeColor','r', ...
        'FontWeight', 'bold','HorizontalAlignment','center');
    zoom('out')
    
 %% MARTINS Gaussfit

    gc=x;                     % Generate short dataset for Gaussian fit
    vFitwidth=floor(pointsongaussianEdit/2);
    gci=find(datax>gc, 1);
    gk=datax(gci-vFitwidth:gci+vFitwidth);
    gd=datay(gci-vFitwidth:gci+vFitwidth);
    ftype = fittype('gauss1');
    foptions = fitoptions('gauss1');
%    foptions.StartPoint = [2e4 gc 0.2];
    gfit = fit(gk,gd,ftype,foptions)
    gf=gfit.a1*exp(-((gk-gfit.b1)/gfit.c1).^2);
   %   gf=(gfit.a1*exp(-((gk-gfit.b1)/gfit.c1).^2))+(gfit.a2*exp(-((gk-gfit.b2)/gfit.c2).^2));
    %gf=10000*exp(-((gk-gc)/.1).^2);

   % Display the fit
   line(gk,gf,'Color','r','Marker','+','Linewidth',2,'Linestyle','none');
       
   

   
    
%% Plot Reference in second subplot
    subplot(2,1,2);
        plot(refx, refy, 'r')
         text(refx(2),refy(2),['\leftarrow',ramanpeak],...
      'HorizontalAlignment','left')
%   setAxesZoomMotion(h,refgraph,'horizontal');
%% anotate Referencespectrum:
% Create textarrow
annotation(chooser,'textarrow',[0.422594142259414 0.422594142259412],...
    [0.276536312849162 0.222067039106146],'TextEdgeColor','none',...
    'String',{'1444 cm^-^1'});
annotation(chooser,'textarrow',[0.821129707112971 0.851782363977483],...
    [0.354748603351955 0.401831308915882],'TextEdgeColor','none',...
    'String',{'2852 cm^-^1'});
annotation(chooser,'textarrow',[0.25836820083682 0.225721619945519],...
    [0.392458100558659 0.391948107358569],'TextEdgeColor','none',...
    'String',{'801 cm^-^1'});
annotation(chooser,'textarrow',[0.334728033472803 0.334728033472803],...
    [0.304469273743017 0.202513966480447],'TextEdgeColor','none',...
    'String',{'1157 cm^-^1'});
annotation(chooser,'textarrow',[0.368200836820084 0.368200836820082],...
    [0.279329608938547 0.222067039106146],'TextEdgeColor','none',...
    'String',{'1266 cm^-^1'});
annotation(chooser,'textarrow',[0.292887029288703 0.292887029288703],...
    [0.277932960893855 0.238826815642458],'TextEdgeColor','none',...
    'String',{'1028 cm^-^1'});

%% Ask for identity of selected peak

    choicelist = {[num2str(ramanpeak0),' cm^-^1'], ...
                    [num2str(ramanpeak1),' cm^-^1'], ...
                    [num2str(ramanpeak2),' cm^-^1'], ...
                    [num2str(ramanpeak3),' cm^-^1'], ...
                    [num2str(ramanpeak4),' cm^-^1'], ...
                    [num2str(ramanpeak5),' cm^-^1']};
    [choice, ~] = listdlg('PromptString','Which peak did you choose?',...
                'Name','Choose Peak', ...
                'SelectionMode','single', ...
                'ListSize', [150 100], ...
                'InitialValue', 2, ...
                'ListString', choicelist);

%% Handle response
    switch choice
        case 1
            ramanpeak = ramanpeak0;
        case 2
            ramanpeak = ramanpeak1;
        case 3
            ramanpeak = ramanpeak2;
        case 4  
            ramanpeak = ramanpeak3;
        case 5
            ramanpeak = ramanpeak4;
        case 6
            ramanpeak = ramanpeak5;
        otherwise 
            errordlg('Callibration failed !!')
    end            

%% close Figures and compute WL
close(chooser)

% ----- Compute LaserWL -----    
LaserWL = (1/((ramanpeak/10000000)+(1/x)));
LaserWLgauss = (1/((ramanpeak/10000000)+(1/gfit.b1)));

%% output Message Dialog with results
results = {['Choosen Raman peak was: ',num2str(ramanpeak),'cm^-^1'],...
            '', ...
            ['Resulting LASER wavelength: ', num2str(LaserWL),' nm'],...
            '',...
            ['Resulting LASER wavelength (Gauss): ',num2str(LaserWLgauss),' nm']};
 q = warndlg(results,...
             'Results', 'modal');
 uiwait(q)